/**
 * 文件名称:  DepartmentDao.java
 * 所属版权:  ChinaStar Technologies Co., Ltd. Copyright 2010-2021, All rights reserved
 * 简单描述:  <描述>
 * 修改作者:  xdli
 * 修改时间:  Jun 29, 2012
 * 修改内容:  <修改内容>
 */
package com.chinastar.iweb.department.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.chinastar.iweb.common.jdbc.DataType;
import com.chinastar.iweb.common.jdbc.ParameType;
import com.chinastar.iweb.common.jdbc.StarSupport;
import com.chinastar.iweb.common.util.Constuls;
import com.chinastar.iweb.department.domain.DepartmentBean;

/**
 * DepartmentDao
 *  
 * @author  xdli
 * @version Jun 29, 2012
 * @see     DepartmentDao
 * @since   1.0
 */
public class DepartmentDao
{
    
    private StarSupport starSupport = null;

    public StarSupport getStarSupport()
    {
        return starSupport;
    }

    public void setStarSupport(StarSupport starSupport)
    {
        this.starSupport = starSupport;
    }
    
    public boolean save(DepartmentBean departmentBean)
    {
        try
        {
            starSupport.setProcedure("P_DEPARTMENT_SAVE(:I_NUM,:I_NAME,:I_STARTS,:O_RET)");
            starSupport.addParame("I_NUM", DataType.VARCHAR, departmentBean.getDepartmentNum(), ParameType.PARAME_IN);
            starSupport.addParame("I_NAME", DataType.VARCHAR, departmentBean.getDepartmentName(), ParameType.PARAME_IN);
            starSupport.addParame("I_STARTS", DataType.INTEGER, departmentBean.getDepartmentStarts(), ParameType.PARAME_IN);
            starSupport.addParame("O_RET", DataType.INTEGER, null, ParameType.PARAME_OUT);
            starSupport.execute();
            return starSupport.getInt("O_RET") == Constuls.NUMBER_O;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }
    
    public boolean update(DepartmentBean departmentBean)
    {
        try
        {
            starSupport.setProcedure("P_DEPARTMENT_UPDATE(:I_NUM,:I_NAME,:I_STARTS,:O_RET)");
            starSupport.addParame("I_NUM", DataType.VARCHAR, departmentBean.getDepartmentNum(), ParameType.PARAME_IN);
            starSupport.addParame("I_NAME", DataType.VARCHAR, departmentBean.getDepartmentName(), ParameType.PARAME_IN);
            starSupport.addParame("I_STARTS", DataType.INTEGER, departmentBean.getDepartmentStarts(), ParameType.PARAME_IN);
            starSupport.addParame("O_RET", DataType.INTEGER, null, ParameType.PARAME_OUT);
            starSupport.execute();
            return starSupport.getInt("O_RET") == Constuls.NUMBER_O;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return false;
    }
    
    public boolean delete(DepartmentBean departmentBean)
    {

        try
        {
            starSupport.setProcedure("P_DEPARTMENMENT_DELETE(:I_DEPARTMENTNUM,:I_TYPE,:O_RET)");
            starSupport.addParame("I_DEPARTMENTNUM", DataType.VARCHAR, departmentBean.getDepartmentNum(), ParameType.PARAME_IN);
            starSupport.addParame("I_TYPE", DataType.INTEGER, Constuls.NUMBER_1, ParameType.PARAME_IN);
            starSupport.addParame("O_RET", DataType.INTEGER, null, ParameType.PARAME_OUT);
            starSupport.execute();
            return starSupport.getInt("O_RET") == Constuls.NUMBER_O;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            starSupport.close();
        }
        return true;
    }
    
    public boolean deleteAll()
    {
        try
        {
            starSupport.setProcedure("P_DEPARTMENMENT_DELETE(:I_DEPARTMENTNUM,:I_TYPE,:O_RET)");
            starSupport.addParame("I_DEPARTMENTNUM", DataType.VARCHAR, null, ParameType.PARAME_IN);
            starSupport.addParame("I_TYPE", DataType.INTEGER, Constuls.NUMBER_O, ParameType.PARAME_IN);
            starSupport.addParame("O_RET", DataType.INTEGER, null, ParameType.PARAME_OUT);
            starSupport.execute();
            return starSupport.getInt("O_RET") == Constuls.NUMBER_O;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            starSupport.close();
        }
        return false;
    }
    
    public DepartmentBean getDepartmentBean(DepartmentBean departmentBean)
    {
        try
        {
            starSupport.setProcedure("P_DEPARTMENT_GET(:I_DepartmentNum,:O_RET,:O_CURSOR)");
            starSupport.addParame("I_DepartmentNum", DataType.VARCHAR, departmentBean.getDepartmentNum(), ParameType.PARAME_IN);
            starSupport.addParame("O_RET", DataType.INTEGER, null, ParameType.PARAME_OUT);
            starSupport.addParame("O_CURSOR", DataType.CURSOR, null, ParameType.PARAME_OUT);
            starSupport.execute();
            if (starSupport.getInt("O_RET") == Constuls.NUMBER_O)
            {
                List<DepartmentBean> departList = getDepartmentList(starSupport.getDataSet("O_CURSOR"));
                departmentBean = departList.get(0);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            starSupport.close();
        }
        return departmentBean;
    }
    
    public List<DepartmentBean> queryList()
    {
        try
        {
            starSupport.setProcedure("P_DEPARTMENT_LIST(:O_RET,:O_CURSOR)");
            starSupport.addParame("O_RET", DataType.INTEGER, null, ParameType.PARAME_OUT);
            starSupport.addParame("O_CURSOR", DataType.CURSOR, null, ParameType.PARAME_OUT);
            starSupport.execute();
            if (starSupport.getInt("O_RET") == Constuls.NUMBER_O)
            {
                return getDepartmentList(starSupport.getDataSet("O_CURSOR"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            starSupport.close();
        }
        return null;
    }
    
    private List<DepartmentBean> getDepartmentList(ResultSet rs)
    {
        List<DepartmentBean> departmentList = new ArrayList<DepartmentBean>();
        try
        {
            while (rs.next())
            {
                DepartmentBean departmentBean = new DepartmentBean();
                departmentBean.setDepartmentNum(rs.getString(""));
                departmentBean.setDepartmentName(rs.getString(""));
                departmentBean.setDepartmentStarts(rs.getString(""));
                departmentList.add(departmentBean);
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            starSupport.close(rs);
        }
        return departmentList;
    }
}
